using CSRedis;
using HDLogCenter.Bll.Services;
using HDLogCenter.Dal.Repos;
using HDLogCenter.Infrastructure.Config;
using HDLogCenter.Infrastructure.ExternalServices;
using HDLogCenter.Infrastructure.Mysql;
using Serilog;

var builder = WebApplication.CreateBuilder(args);

// 配置Serilog
Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(builder.Configuration)
    .Enrich.FromLogContext()
    .WriteTo.Console()
    .WriteTo.File("Logs/log-.txt", rollingInterval: RollingInterval.Day)
    .CreateLogger();

builder.Host.UseSerilog();

Dapper.SimpleCRUD.SetDialect(Dapper.SimpleCRUD.Dialect.MySQL);
Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;
// Add services to the container.
builder.Services.AddControllersWithViews();

// 配置选项
builder.Services.Configure<ConnectionStrings>(builder.Configuration.GetSection("ConnectionStrings"));
builder.Services.Configure<MeiliSearchConfig>(builder.Configuration.GetSection("MeiliSearch"));

// 注册服务
builder.Services.AddScoped<IDbService, DbService>();

// 注册 MeiliSearch 服务
builder.Services.AddScoped<MeiliSearchService>();

// 注册 HTTP 客户端（如果需要）
builder.Services.AddHttpClient();

// 注册健康检查服务
builder.Services.AddHealthChecks();

var connectionStrings = builder.Configuration.GetSection("ConnectionStrings").GetSection("Redis").Value;
RedisHelper.Initialization(new CSRedisClient(connectionStrings));

// 注册仓储
builder.Services.AddScoped<DbService>();
builder.Services.AddScoped<AppConfigRepo>();
builder.Services.AddScoped<AlertRuleRepo>();

// 注册业务服务
builder.Services.AddScoped<LogService>();
builder.Services.AddScoped<ProjectConfigService>();
builder.Services.AddScoped<AlertRuleService>();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

// 添加健康检查端点
app.MapHealthChecks("/health");

app.Run();
